import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.*;


public class ClientGUI {
		
	private int auto;
	private JButton forward, backward, turnLeft, turnRight, scan, autoMode;
	private JTextField numberInput;
	private JFrame mapFrame, camFrame, controlFrame;
	private JPanel panel;
	private JLabel webcam, map;
	private Images images;
	private JTextField host, port, robot;
	private XMLHandler outXML;
	private boolean connectStatus;
	
	public ClientGUI(Images images) 
	{
		this.auto = 0;
		this.images = images; 
		this.connectStatus = false;
		host = new JTextField(10);
	    port = new JTextField(5);
	    robot = new JTextField(10);
		
	    webcam = new JLabel(new ImageIcon(images.getWebcamImage()));
		map = new JLabel(new ImageIcon(images.getMapImage()));
		
		GridBagConstraints c = new GridBagConstraints();
		panel = new JPanel(new GridBagLayout());
		mapFrame = new JFrame("Map");
		camFrame = new JFrame("Camera");
		controlFrame = new JFrame("Controls");
		
		forward = new JButton("Forward");
		backward = new JButton("Backward");
		turnLeft = new JButton("Left");
		turnRight = new JButton("Right");
		autoMode = new JButton("Auto");
		scan = new JButton("Scan");
		numberInput = new JTextField(5);
		
		numberInput.setText("10");
		
		panel.setLayout(new GridLayout(3,3));
		panel.add(new JLabel(""));
		panel.add(forward);
		panel.add(new JLabel(""));
		panel.add(turnLeft);
		panel.add(autoMode);
		panel.add(turnRight);
		panel.add(numberInput);
		panel.add(backward);
		panel.add(scan);

		camFrame.getContentPane().add(webcam, BorderLayout.CENTER);
		mapFrame.getContentPane().add(map, BorderLayout.CENTER);
		controlFrame.getContentPane().add(panel, BorderLayout.CENTER);
		
		controlFrame.pack();
		
		mapFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		camFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		controlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		camFrame.setVisible(true);
		mapFrame.setVisible(true);
		controlFrame.setVisible(true);
	}
	
	
	
	public String getNumberInput()
	{
		return numberInput.getText();
	}
	
	//adds action listener
	public void addActionListener(ActionListener listener)
	{
		this.forward.addActionListener(listener);
		this.backward.addActionListener(listener);
		this.turnLeft.addActionListener(listener);
		this.turnRight.addActionListener(listener);
		this.scan.addActionListener(listener);
		this.autoMode.addActionListener(listener);
	}
	
	public int getInputValue() {
		return Integer.valueOf(numberInput.getText());
	}
	
	public JButton getForwardButton()
	{
		return this.forward;
	}
	
	public JButton getBackwardButton()
	{
		return this.backward;
	}
	
	public JButton getTurnLeftButton()
	{
		return this.turnLeft;
	}
	

	public JButton getScanButton() {
		return this.scan;
	}
	
	public JButton getTurnRightButton()
	{
		return this.turnRight;
	}
	
	public JButton getAutoButton()
	{
		if(this.auto == 0){
			this.forward.setEnabled(false);
			this.backward.setEnabled(false);
			this.turnLeft.setEnabled(false);
			this.turnRight.setEnabled(false);
			this.scan.setEnabled(false);
			this.setAutoStatus(1);
		}else{
			this.forward.setEnabled(true);
			this.backward.setEnabled(true);
			this.turnLeft.setEnabled(true);
			this.turnRight.setEnabled(true);
			this.scan.setEnabled(true);
			this.setAutoStatus(0);
		}
		return this.autoMode;
	}
	
	public int getAutoStatus() {
		return this.auto;
	}
	
	public void setAutoStatus(int status) {
		this.auto = status;
	}
	
	public void update() {
		map.setIcon(new ImageIcon(images.getMapImage()));
		mapFrame.pack();
		map.repaint();
		mapFrame.repaint();
		webcam.setIcon(new ImageIcon(images.getWebcamImage()));
		camFrame.pack();
		webcam.repaint();
		mapFrame.repaint();
	}
	
	public String getHost() {
		return this.host.getText();
	}
	
	public String getPort() {
		return this.port.getText();
	}
	
	public String getRobotName() {
		return this.robot.getText();
	}
	
	public void startScreen() {

	      JPanel myPanel = new JPanel();
	      myPanel.add(new JLabel("Address:"));
	      host.setText("localhost");
	      myPanel.add(host);
	      myPanel.add(new JLabel("Port:"));
	      port.setText("4444");
	      myPanel.add(port);
	      myPanel.add(new JLabel("Robot:"));
	      robot.setText("Helge");
	      myPanel.add(robot);

	      int result = JOptionPane.showConfirmDialog(null, myPanel, 
	               "Connect", JOptionPane.OK_CANCEL_OPTION);
	      if(result == JOptionPane.OK_OPTION) {
	    	  this.connectStatus = true;
	      }else{
	    	  System.exit(0);
	      }
	}
	
	public boolean getconnectStatus() {
		return this.connectStatus;
	}
	
	/*
	public void displayErrorMessage(String str)
	{
		JOptionPane.showMessageDialog(this, str);
	} */

}
